#include "StdAfx.h"
#include "AutoArchive.h"

CAutoArchive::CAutoArchive(void)
{
}

CAutoArchive::~CAutoArchive(void)
{
}


/////////////////////////////////////////////////////////////////////////////
// Swap1 the byte order
// htonl htons ntohl ntohs
void Swap1(void* pData, int size)
{
	/* HAWK0507
	 * Bad port work!!! Whether or not swap should be base on running machine's
	 * byte order, not base on OS type!!!
	 */
#if 0
#ifndef UNIX
    unsigned char* pch = (unsigned char*)pData;

    for(int i = 0; i < size / 2; i++)
    {
        unsigned char chTemp;
        chTemp = pch[i];
        pch[i] = pch[size-1-i];
        pch[size-1-i] = chTemp;
    }
#endif  //UNIX
#endif

	int				i;
	int				flag = 0x1;
	char*			pFlag;
	unsigned char	chTemp;
	unsigned char*	pch;

	pFlag = (char*)(&flag);
	if( *pFlag == 1 )
	{
		/* Little endian byte order, do swap */
		pch = (unsigned char*) pData;
		for(i=0; i<size/2; i++)
		{
			chTemp = pch[i];
			pch[i] = pch[size-i-1];
			pch[size-i-1] = chTemp;
		}
	}
}

/////////////////////////////////////////////////////////////////////////////
// CByteStream
//
CByteStream::CByteStream()
{

}
CByteStream::~CByteStream()
{

}
CByteStream::CByteStream(unsigned char* pBuf)
{
    //ASSERT(pBuf);
    m_pBuf = pBuf;
    m_lCurPos = 0;
}

long CByteStream::Seek(long lPos)
{
    long lOld = m_lCurPos;
    m_lCurPos = lPos;
    return lOld;
}

long CByteStream::Tell()
{
    return m_lCurPos;
}

unsigned char* CByteStream::GetData()
{
    return m_pBuf;
}

void CByteStream::Write(void* pData, long lSize)
{
    if (lSize == 0)
        return;

    //ASSERT(pData);

    memcpy(m_pBuf + m_lCurPos, pData, lSize);
    m_lCurPos += lSize;
}

void CByteStream::Read(void* pData, long lSize)
{
    if (lSize == 0)
        return;

    //ASSERT(pData);

    memcpy(pData, m_pBuf + m_lCurPos, lSize);
    m_lCurPos += lSize;
}

CByteStream& CByteStream::operator << (unsigned char ch)
{
    m_pBuf[m_lCurPos++] = ch;
    return *this;
}

CByteStream& CByteStream::operator << (char ch)
{
    m_pBuf[m_lCurPos++] = (unsigned char)ch;
    return *this;
}

CByteStream& CByteStream::operator << (short s)
{
    Swap1(&s, sizeof(short));
    Write(&s, sizeof(short));
    return *this;
}

CByteStream& CByteStream::operator << (unsigned short s)
{
    Swap1(&s, sizeof(unsigned short));
    Write(&s, sizeof(unsigned short));
    return *this;
}

CByteStream& CByteStream::operator << (int i)
{
    Swap1(&i, sizeof(int));
    Write(&i, sizeof(int));
    return *this;
}

CByteStream& CByteStream::operator << (unsigned int i)
{
    Swap1(&i, sizeof(unsigned int));
    Write(&i, sizeof(unsigned int));
    return *this;
}

CByteStream& CByteStream::operator << (long l)
{
    Swap1(&l, sizeof(long));
    Write(&l, sizeof(long));
    return *this;
}

CByteStream& CByteStream::operator << (unsigned long l)
{
    Swap1(&l, sizeof(unsigned long));
    Write(&l, sizeof(unsigned long));
    return *this;
}

CByteStream& CByteStream::operator << (float f)
{
    Swap1(&f, sizeof(float));
    Write(&f, sizeof(float));
    return *this;
}

CByteStream& CByteStream::operator << (double d)
{
    Swap1(&d, sizeof(double));
    Write(&d, sizeof(double));
    return *this;
}

CByteStream& CByteStream::operator >> (unsigned char& ch)
{
    ch = m_pBuf[m_lCurPos++];
    return *this;
}

CByteStream& CByteStream::operator >> (char& ch)
{
    ch = (char)m_pBuf[m_lCurPos++];
    return *this;
}

CByteStream& CByteStream::operator >> (short& s)
{
    Read(&s, sizeof(short));
    Swap1(&s, sizeof(short));
    return *this;
}

CByteStream& CByteStream::operator >> (unsigned short& s)
{
    Read(&s, sizeof(unsigned short));
    Swap1(&s, sizeof(unsigned short));
    return *this;
}

CByteStream& CByteStream::operator >> (int& i)
{
    Read(&i, sizeof(int));
    Swap1(&i, sizeof(int));
    return *this;
}

CByteStream& CByteStream::operator >> (unsigned int& i)
{
    Read(&i, sizeof(unsigned int));
    Swap1(&i, sizeof(unsigned int));
    return *this;
}

CByteStream& CByteStream::operator >> (long& l)
{
    Read(&l, sizeof(long));
    Swap1(&l, sizeof(long));
    return *this;
}

CByteStream& CByteStream::operator >> (unsigned long& l)
{
    Read(&l, sizeof(unsigned long));
    Swap1(&l, sizeof(unsigned long));
    return *this;
}

CByteStream& CByteStream::operator >> (float& f)
{
    Read(&f, sizeof(float));
    Swap1(&f, sizeof(float));
    return *this;
}

CByteStream& CByteStream::operator >> (double& d)
{
    Read(&d, sizeof(double));
    Swap1(&d, sizeof(double));
    return *this;
}

unsigned char CByteStream::ReadUChar(unsigned char* pBuf, long off)
{
    return pBuf[off];
}

char CByteStream::ReadChar(unsigned char* pBuf, long off)
{
    return (char)pBuf[off];
}

unsigned short CByteStream::ReadUShort(unsigned char* pBuf, long off)
{
    unsigned short s;
    memcpy(&s, pBuf + off, sizeof(unsigned short));
    Swap1(&s, sizeof(unsigned short));

    return s;
}

short CByteStream::ReadShort(unsigned char* pBuf, long off)
{
    short s;
    memcpy(&s, pBuf + off, sizeof(short));
    Swap1(&s, sizeof(short));

    return s;
}

unsigned int CByteStream::ReadUInt(unsigned char* pBuf, long off)
{
    unsigned int i;
    memcpy(&i, pBuf + off, sizeof(unsigned int));
    Swap1(&i, sizeof(unsigned int));

    return i;
}

int CByteStream::ReadInt(unsigned char* pBuf, long off)
{
    int i;
    memcpy(&i, pBuf + off, sizeof(int));
    Swap1(&i, sizeof(int));

    return i;
}

unsigned long CByteStream::ReadULong(unsigned char* pBuf, long off)
{
    unsigned long l;
    memcpy(&l, pBuf + off, sizeof(unsigned long));
    Swap1(&l, sizeof(unsigned long));

    return l;
}

long CByteStream::ReadLong(unsigned char* pBuf, long off)
{
    long l;
    memcpy(&l, pBuf + off, sizeof(long));
    Swap1(&l, sizeof(long));

    return l;
}

float CByteStream::ReadFloat(unsigned char* pBuf, long off)
{
    float f;
    memcpy(&f, pBuf + off, sizeof(float));
    Swap1(&f, sizeof(float));

    return f;
}

double CByteStream::ReadDouble(unsigned char* pBuf, long off)
{
    double d;
    memcpy(&d, pBuf + off, sizeof(double));
    Swap1(&d, sizeof(double));

    return d;
}

int CByteStream::WriteUChar(unsigned char* pBuf, long off, unsigned char ch)
{
    Swap1(&ch, sizeof(unsigned char));
    memcpy(pBuf + off, &ch, sizeof(unsigned char));

    return sizeof(unsigned char);
}

int CByteStream::WriteChar(unsigned char* pBuf, long off, char ch)
{
    Swap1(&ch, sizeof(char));
    memcpy(pBuf + off, &ch, sizeof(char));

    return sizeof(char);
}

int CByteStream::WriteUShort(unsigned char* pBuf, long off, unsigned short s)
{
    Swap1(&s, sizeof(unsigned short));
    memcpy(pBuf + off, &s, sizeof(unsigned short));

    return sizeof(unsigned short);
}

int CByteStream::WriteShort(unsigned char* pBuf, long off, short s)
{
    Swap1(&s, sizeof(short));
    memcpy(pBuf + off, &s, sizeof(short));

    return sizeof(short);
}

int CByteStream::WriteUInt(unsigned char* pBuf, long off, unsigned int i)
{
    Swap1(&i, sizeof(unsigned int));
    memcpy(pBuf + off, &i, sizeof(unsigned char));

    return sizeof(unsigned int);
}

int CByteStream::WriteInt(unsigned char* pBuf, long off, int i)
{
    Swap1(&i, sizeof(int));
    memcpy(pBuf + off, &i, sizeof(int));

    return sizeof(int);
}

int CByteStream::WriteULong(unsigned char* pBuf, long off, unsigned long l)
{
    Swap1(&l, sizeof(unsigned long));
    memcpy(pBuf + off, &l, sizeof(long));

    return sizeof(long);
}

int CByteStream::WriteLong(unsigned char* pBuf, long off, long l)
{
    Swap1(&l, sizeof(long));
    memcpy(pBuf + off, &l, sizeof(long));

    return sizeof(long);
}

int CByteStream::WriteFloat(unsigned char* pBuf, long off, float f)
{
    Swap1(&f, sizeof(float));
    memcpy(pBuf + off, &f, sizeof(float));

    return sizeof(float);
}

int CByteStream::WriteDouble(unsigned char* pBuf, long off, double d)
{
    Swap1(&d, sizeof(double));
    memcpy(pBuf + off, &d, sizeof(double));

    return sizeof(double);
}

void CByteStream::SetType(long l) 
{
	Swap1(&l, sizeof(long));
	memcpy(m_pBuf, &l, sizeof(long));
}

//////////////////////////////////////////////////////////////////////////
/*
CMyStream::CMyStream()
{

}
CMyStream::~CMyStream()
{

}
CMyStream::CMyStream(char *pBuf):CByteStream((unsigned char*)pBuf)
{
	
}



void CMyStream::SetText(char *text)
{
	if (text!=0&&strlen(text)>0)
	{
		long len = strlen(text);
		Swap1(&len, sizeof(long));
		Write(&len, sizeof(long));

		Write(text, len);
	}
}

void CMyStream::GetText(char *text, long len)
{
	if (len == 0)
	{
		long l = 0;
		Read(&l, sizeof(long));
		Swap1(&l, sizeof(long));
		len = l;
	}
	else if (text != NULL)
	{
		Read(text, len);
	}

}
*/
//////////////////////////////////////////////////////////////////////////

#ifdef WIN32
// CUnicodeTool
void CUnicodeTool::Unicode2MultiByte(IN wchar_t* pUnicodeText, OUT char* pMultiByteText)
{
	USES_CONVERSION;
	pMultiByteText = W2A(pUnicodeText);
}
void CUnicodeTool::MultiByte2Unicode(IN char* pMultiByteText, OUT wchar_t* pUnicodeText)
{
	USES_CONVERSION;
	pUnicodeText = A2W(pMultiByteText);
}


#endif

